package com.yuand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuand.entity.SysMenu;
import com.yuand.entity.SysRoleMenu;
import com.yuand.mapper.SysMenuMapper;
import com.yuand.mapper.SysRoleMenuMapper;
import com.yuand.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author yuandong
 * @since 2022-01-15
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    /**
     * 根据用户查询系统菜单列表
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Override
    public List<SysMenu> selectMenuList(Long userId) {

        return selectMenuList(new SysMenu(), userId);
    }

    /**
     * 根据用户查询该用户对应的菜单权限，包含按钮
     *
     * @return
     */
    @Override
    public List<SysMenu> selectMenuList(SysMenu menu, Long userId) {

        List<SysMenu> menuList = null;
        if (userId.equals(1L)) {
            //管理员查询所有
            menuList = sysMenuMapper.selectMenuList(menu);
        } else {
            //普通用户根据该用户id查询
            menu.getParams().put("userId", userId);
            menuList = sysMenuMapper.selectMenusByUserId(menu);
        }
        return menuList;
    }


    /**
     * 判断菜单名是否重复
     *
     * @param sysMenu
     * @return
     */
    public boolean checkMenuNameUnique(SysMenu sysMenu) {
        List<SysMenu> list = this.sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("menu_name", sysMenu.getMenuName()).eq("parent_id", sysMenu.getParentId()).last("limit 1"));
        if (list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 保存菜单
     *
     * @param sysMenu
     * @return
     */
    public int addMenu(SysMenu sysMenu) {
        sysMenu.setDelFlag(0);
        return this.sysMenuMapper.insert(sysMenu);
    }


    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    public int updateMenu(SysMenu sysMenu) {
        return this.sysMenuMapper.updateById(sysMenu);
    }


    /**
     * 根据menuId查询菜单
     *
     * @param menuId
     * @return
     */
    public SysMenu selectByMenuId(Long menuId) {
        return this.sysMenuMapper.selectOne(new QueryWrapper<SysMenu>().eq("menu_id", menuId));
    }


    /**
     * 查询该菜单是否含有子菜单
     *
     * @param menuId
     * @return
     */
    public boolean hasChildByMenuId(Long menuId) {
        int flag = this.sysMenuMapper.selectCount(new QueryWrapper<SysMenu>().eq("parent_id", menuId));
        return flag > 0 ? true : false;
    }


    /**
     * 查询该菜单是否已经关联角色
     * @param menuId
     */
    public boolean checkMenuExistRole(Long menuId){
        Integer flag = this.sysRoleMenuMapper.selectCount(new QueryWrapper<SysRoleMenu>().eq("menu_id", menuId));
        return flag > 0 ? true : false;
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    public int  delMenu(Long menuId){
      return   this.sysMenuMapper.deleteById(menuId);
    }


}
